<template>
  <div class="menu-container">
    <div class="menu-search">
      <div class="menu-left">
        <el-form :model="searchForm" inline ref="searchRef">
          <el-form-item label="食堂" prop="canteen">
            <el-select
              v-model="searchForm.canteen"
              placeholder="请选择食堂"
              size="small"
            >
              <el-option
                v-for="item in canteenOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option> </el-select
          ></el-form-item>
          <el-form-item label="时间区间">
            <el-date-picker
              size="small"
              v-model="searchTime"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="YYYY-MM-DD"
            >
            </el-date-picker
          ></el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              size="small"
              icon="el-icon-search"
              @click="search"
              >搜索</el-button
            >
            <el-button size="small" icon="el-icon-refresh-left" @click="reset"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <div class="menu-right">
        <el-button type="primary" size="small" @click="submitMenuAudit"
          >审核</el-button
        >
      </div>
    </div>
    <!--    表格区域-->
    <div class="table" style="height: calc(100% - 160px); overflow-y: auto">
      <el-table
        :header-cell-style="{ background: '#FEF6F6' }"
        ref="multipleTable"
        :data="menuTableData"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="canteen" label="食堂">
          <template #default="scope">
            <span>{{ scope.row.canteen.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="日期">
          <template #default="scope">
            <span>{{ scope.row.beginTime }} - {{ scope.row.endTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态">
          <template #default="scope">
            <span v-if="scope.row.status === 1">待提交</span>
            <span v-if="scope.row.status === 2">待审核</span>
            <span v-if="scope.row.status === 3">已下架</span>
            <span v-if="scope.row.status === 4">在售中</span>
            <span v-if="scope.row.status === 5">驳回</span>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="description"
          label="描述"
        />
        <el-table-column label="操作">
          <template #default="scope">
            <el-button
              icon="el-icon-view"
              type="text"
              size="small"
              @click="audit(scope.row.id)"
              >审核</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      style="float: right; margin: 20px"
      @size-change="pageSizeChange"
      @current-change="pageCurrentChange"
      :current-page="pageNum"
      :page-size="pageSize"
      layout=" prev, pager, next"
      :total="totalNum"
      background
    >
    </el-pagination>
    <!-- 批量审核对话框 -->
    <el-dialog
      v-model="menuAuditDialogVisible"
      title="菜谱批量审核"
      width="30%"
    >
      <el-input
        v-model="auditContent"
        placeholder="请输入审核意见"
        type="textarea"
      ></el-input>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="success" @click="submitAuit" size="small"
            >通过</el-button
          >
          <el-button type="danger" @click="rejetAuit" size="small"
            >驳回</el-button
          >
          <el-button @click="menuAuditDialogVisible = false" size="small"
            >取消</el-button
          >
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { onMounted, ref } from "vue";
import { getCanteenList } from "../../../../api/canteen/menu-managerApi";
import { menuAudit, menuMultipleSubmit } from "@/api/canteen/audit";
import commonPageRepository from "../../../../composables/common-page-repository";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";

export default {
  name: "MenuAudit",
  setup() {
    const searchForm = ref({});
    const canteenOptions = ref([]);
    //获取食堂列表
    function getCanteen() {
      getCanteenList().then((res) => {
        canteenOptions.value = res.data;
      });
    }
    // 获取菜谱审核列表
    function getMenuAuditList() {
      menuAudit({
        pageNum: pageNum.value,
        pageSize: pageSize.value,
      }).then((res) => {
        menuTableData.value = res.data.content;
        totalNum.value = res.data.totalElements;
      });
    }
    const menuTableData = ref([]);
    const pageNum = ref(1);
    const pageSize = ref(50);
    const totalNum = ref("");
    const { pageSizeChange, pageCurrentChange } = commonPageRepository(
      pageSize,
      pageNum,
      getMenuAuditList
    );
    const router = useRouter();
    function audit(id) {
      router.push({
        name: "menuAudit",
        query: {
          id,
        },
      });
    }
    const searchTime = ref("");
    // 搜索表单标识
    const searchRef = ref(null);
    // 搜索功能
    function search() {
      let startTime = searchTime.value[0];
      let endTime = searchTime.value[1];
      pageNum.value = 1;
      menuAudit({
        pageNum: pageNum.value,
        pageSize: pageSize.value,
        canteenId: searchForm.value.canteen,
        startTime,
        endTime,
      }).then((res) => {
        menuTableData.value = res.data.content;
        totalNum.value = res.data.totalElements;
      });
    }
    // 重置操作
    function reset() {
      searchRef.value.resetFields();
      searchTime.value = "";
      getMenuAuditList();
    }
    // 表格多选ID集合
    const ids = ref([]);
    // 多选数据集合
    const selectArray = ref([]);
    // 表格多选操作
    function handleSelectionChange(val) {
      selectArray.value = val;
    }
    // 审核对话框显示与隐藏
    const menuAuditDialogVisible = ref(false);
    // 菜谱审核
    function submitMenuAudit() {
      if (selectArray.value.length > 0) {
        menuAuditDialogVisible.value = true;
      } else {
        ElMessage.warning("请选择数据后再试！");
      }
    }
    // 审核原因
    const auditContent = ref("");
    // 通过审核
    function submitAuit() {
      selectArray.value.map((item) => {
        ids.value.push(item.id);
      });
      if (auditContent.value) {
        menuMultipleSubmit({
          auditFlag: true,
          auditReason: auditContent.value,
          ids: ids.value.join(","),
        })
          .then(() => {
            ElMessage.success("批量审核成功");
            menuAuditDialogVisible.value = false;
            auditContent.value = "";
            getMenuAuditList();
          })
          .catch(() => {
            menuAuditDialogVisible.value = false;
            auditContent.value = "";
            getMenuAuditList();
          });
      } else {
        ElMessage.warning("请填写审核意见");
      }
    }
    // 驳回审核
    function rejetAuit() {
      selectArray.value.map((item) => {
        ids.value.push(item.id);
      });
      if (auditContent.value) {
        menuMultipleSubmit({
          auditFlag: true,
          auditReason: auditContent.value,
          ids: ids.value.join(","),
        })
          .then(() => {
            ElMessage.success("驳回成功");
            menuAuditDialogVisible.value = false;
            auditContent.value = "";
            getMenuAuditList();
          })
          .catch(() => {
            menuAuditDialogVisible.value = false;
            auditContent.value = "";
            getMenuAuditList();
          });
      } else {
        ElMessage.warning("请填写审核意见");
      }
    }
    onMounted(() => {
      getCanteen();
      getMenuAuditList();
    });
    return {
      searchForm,
      menuTableData,
      canteenOptions,
      pageSizeChange,
      pageCurrentChange,
      pageSize,
      pageNum,
      totalNum,
      audit,
      searchRef,
      search,
      searchTime,
      reset,
      handleSelectionChange,
      submitMenuAudit,
      menuAuditDialogVisible,
      submitAuit,
      auditContent,
      rejetAuit,
    };
  },
};
</script>

<style scoped lang="scss">
.menu-container {
  .menu-search {
    display: flex;
    justify-content: space-between;
    .menu-right {
      margin-top: 4px;
    }
  }
}
</style>
